package com.shijiwei.trip.fragment;

import android.os.Bundle;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.shijiwei.trip.R;
import com.shijiwei.trip.adapter.CityAdapter;
import com.shijiwei.trip.bean.cityBean.City;
import com.shijiwei.trip.utils.UrlConstants;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sjw on 2015/12/9.
 */
@ContentView(R.layout.fragment_city)
public class CityFragment extends AppBaseFragment {
    @ViewInject(R.id.lv_city)
    private ListView listView;

    private City city;
    private CityAdapter adapter;
    private List<City.ProductListEntity> list;


    public static CityFragment newInstance() {
        
        Bundle args = new Bundle();
        
        CityFragment fragment = new CityFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void initView() {
        listView.setVerticalScrollBarEnabled(false);
        listView.setDivider(null);
        list = new ArrayList<>();
    }

    @Override
    public void initData() {
        x.http().get(new RequestParams(UrlConstants.URL_CITY_GET), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                city = JSONObject.parseObject(result,City.class);
                list.addAll(city.getProduct_list());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                adapter = new CityAdapter(getActivity(),list);
                listView.setAdapter(adapter);
//                adapter.notifyDataSetChanged();
            }
        });
    }


}
